/**
 * 
 */
package com.cr.backbone.util.online;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.cr.backbone.util.UtilsDateTime;

/**
 * 会话监听
 * @project RaedNote
 * @version 1.0
 * @company 
 * @fileDescribe
 * @fileType OnlineSessionLister OnlineSessionLister.java
 * @author xianghua.hu
 * @date 2012-5-16 下午02:06:37
 */
/**
 * @author huxh
 *
 */
public class OnlineSessionLister implements HttpSessionListener,
		HttpSessionAttributeListener {

	
	
	
	//org.apache.commons.logging.LogFactory
	private static final Log log=LogFactory.getLog(OnlineSessionLister.class);

	private int onlineCount;
	private Map<String, OnlineInfo> onlines=new HashMap<String,OnlineInfo>();
	
	
	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpSessionListener#sessionCreated(javax.servlet.http.HttpSessionEvent)
	 */
	public void sessionCreated(HttpSessionEvent se) {
		// TODO Auto-generated method stub
		String sessionId=se.getSession().getId();
		String time = UtilsDateTime.getCurrentDateTimeString();
		log.info("会话建立："+sessionId+"\t"+time);
		System.out.println("会话创建："+sessionId+"\t"+time);
	}

	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpSessionListener#sessionDestroyed(javax.servlet.http.HttpSessionEvent)
	 */
	public void sessionDestroyed(HttpSessionEvent se) {
		String time = UtilsDateTime.getCurrentDateTimeString();
		String sessionId=se.getSession().getId();
		onlineCount--;
		log.info("会话销毁："+sessionId+"\t"+time);
		log.info("当前在线："+onlineCount);
		System.out.println("会话销毁："+sessionId+"\t"+time+"\t onlineCount:"+onlineCount);
	}

	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpSessionAttributeListener#attributeAdded(javax.servlet.http.HttpSessionBindingEvent)
	 */
	public void attributeAdded(HttpSessionBindingEvent se) {
		String time = UtilsDateTime.getCurrentDateTimeString();
		String sessionId=se.getSession().getId();
		onlineCount++;
		log.info("会话创建："+sessionId+"\t"+time);
		log.info("当前在线："+onlineCount);
		System.out.println("会话创建："+sessionId+"\t"+time+"\t onlineCount:"+onlineCount);
	}

	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpSessionAttributeListener#attributeRemoved(javax.servlet.http.HttpSessionBindingEvent)
	 */
	public void attributeRemoved(HttpSessionBindingEvent se) {
		String time = UtilsDateTime.getCurrentDateTimeString();
		String sessionId=se.getSession().getId();
		onlineCount--;
		log.info("会话销毁："+sessionId+"\t"+time);
		log.info("当前在线："+onlineCount);

		System.out.println("会话销毁："+sessionId+"\t"+time+"\t onlineCount:"+onlineCount);
	}

	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpSessionAttributeListener#attributeReplaced(javax.servlet.http.HttpSessionBindingEvent)
	 */
	public void attributeReplaced(HttpSessionBindingEvent se) {
		// TODO Auto-generated method stub

	}

}
 